package com.leostadyn.stackfarm.timestatistics.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.leostadyn.stackfarm.timestatistics.R;
import com.leostadyn.stackfarm.timestatistics.model.bean.UserListBean;
import com.leostadyn.stackfarm.timestatistics.utils.TimeUtils;
import com.leostadyn.stackfarm.timestatistics.view.activity.admin.staff.StaffChangePwdActivity;
import com.leostadyn.stackfarm.timestatistics.view.activity.admin.staff.StaffEditActivity;

import java.util.HashMap;
import java.util.List;

public class StaffListAdapter extends RecyclerView.Adapter{

    private Context mContext;
    private List<UserListBean> staffListBeans;
    private int count = 10;
    private HashMap<Integer, Object> mPosion = new HashMap<>();
    public int getCount() {
        return count;
    }
    public void setCount(int count) {
        this.count = count;
    }


    public StaffListAdapter(Context mContext, List<UserListBean> staffListBeans, HashMap<Integer, Object> mPosion, OnDeleteButtonClick onDeleteButtonClick) {
        this.mContext = mContext;
        if (staffListBeans != null) {
            this.staffListBeans = staffListBeans;
        }
        this.mPosion = mPosion;
        this.onDeleteButtonClick = onDeleteButtonClick;
    }

    private OnDeleteButtonClick onDeleteButtonClick;

    public List<UserListBean> getStaffListBeans() {
        return staffListBeans;
    }

    public void setStaffListBeans(List<UserListBean> staffListBeans) {
        //先清空数据
        this.staffListBeans.clear();
        if(staffListBeans!=null&&staffListBeans.size()>0){
            this.staffListBeans.addAll(staffListBeans);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_staff_list, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {

        if (viewHolder instanceof MyViewHolder) {
            UserListBean data = staffListBeans.get(i);
            ((MyViewHolder) viewHolder).tv_staff_name.setText(data.getName());
            ((MyViewHolder) viewHolder).tv_satff_id.setText(data.getUserName());
            ((MyViewHolder) viewHolder).tv_staff_job.setText(data.getPostName());
            ((MyViewHolder) viewHolder).tv_staff_time.setText(TimeUtils.getTime1(data.getCreatedTime()));
            //删除
            ((MyViewHolder) viewHolder).tv_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onDeleteButtonClick.onBtnClicked(i, data, ((MyViewHolder) viewHolder).tv_staff_name, ((MyViewHolder) viewHolder).tv_staff_job,
                            ((MyViewHolder) viewHolder).tv_satff_id, ((MyViewHolder) viewHolder).tv_staff_time);
                }
            });
            //修改密码
            ((MyViewHolder) viewHolder).tv_change.setOnClickListener(v->{
                Intent intent=new Intent(mContext, StaffChangePwdActivity.class);
                intent.putExtra("uid", data.getUid());
                intent.putExtra("username", data.getUserName());
                mContext.startActivity(intent);

            });
            //编辑
            ((MyViewHolder) viewHolder).tv_edit.setOnClickListener(v->{
                Intent intent=new Intent(mContext, StaffEditActivity .class);
                intent.putExtra("uid", data.getUid());
                intent.putExtra("name", data.getName());
                intent.putExtra("job", data.getPostName());
                intent.putExtra("username", data.getUserName());
                mContext.startActivity(intent);

            });


        }
    }

    @Override
    public int getItemCount() {
        if (staffListBeans == null) {
            return 0;
        }
        return staffListBeans.size();
    }
    @Override
    public int getItemViewType(int position) {
        if (position==staffListBeans.size()){
            return 0;
        }else {
            return 1;
        }
    }
    class MyViewHolder extends RecyclerView.ViewHolder{
        private TextView tv_staff_name;
        private TextView tv_satff_id;
        private TextView tv_staff_job;
        private TextView tv_staff_time;
        private TextView tv_change;
        private TextView tv_edit;
        private TextView tv_delete;
        private LinearLayout layout_staff_detail;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_staff_name=itemView.findViewById(R.id.tv_name);
            tv_staff_job=itemView.findViewById(R.id.tv_staff_job);
            tv_satff_id=itemView.findViewById(R.id.tv_id);
            tv_staff_time=itemView.findViewById(R.id.tv_staff_time);
            tv_change=itemView.findViewById(R.id.tv_change);
            tv_edit=itemView.findViewById(R.id.tv_edit);
            tv_delete=itemView.findViewById(R.id.tv_delete);

        }
    }

    public interface OnDeleteButtonClick{
        void onBtnClicked(int position, UserListBean data, TextView name, TextView job, TextView id, TextView time);
    }
}
